package com.reform.service.impl;

import com.reform.config.JwtTokenUtil;
import com.reform.pojo.admin.Admin;
import com.reform.pojo.common.Nav;
import com.reform.mapper.NavMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IAdminService;
import com.reform.service.INavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.service.ITeacherService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-06-27
 */
@Service
@Log4j
public class NavServiceImpl extends ServiceImpl<NavMapper, Nav> implements INavService {

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @Autowired
    IAdminService adminService;
    @Autowired
    ITeacherService teacherService;

    @Autowired
    NavMapper navMapper;


    @Override
    public Result getNas(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.failure(ResultCode.USER_AUTH_Login_LAPSE);
        }
        Integer type = jwtTokenUtil.getTypeFromToken(token);
        String gh = jwtTokenUtil.getGHFromToken(token);
        //教师导航权限为1
        Integer nav_type = 1;
        //专家导航权限为2
        if (type == 2) {
            nav_type++;
        }
        //管理员权限分为查询与操作管理员
        if (type == 3) {
            //基本院查询管理员为3
            nav_type = 3;
            Admin admin = adminService.getAdminByGh(gh);
            Integer depId = admin.getDepId();
            //查询管理员此处为0，操作管理员此处为1
            Integer adminType = admin.getType();
            //校查询管理员为5
            if (depId == 0) {
                nav_type = 5;
            }
            //暨权限分为 3（院查） 4（院操） 5（校查） 6（校操）
            nav_type += adminType;
        }
        // 数据库中对应的
        return Result.success(ResultCode.SUCCESS, navMapper.getNavsByType(nav_type));

    }

    @Override
    public Result getNavById(HttpServletRequest request, Integer type) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.failure(ResultCode.USER_AUTH_Login_LAPSE);
        }
        String gh = jwtTokenUtil.getGHFromToken(token);
        //教师导航权限为1
        Integer nav_type = 1;

        //判断是否专家
        //专家导航权限为2
        if (type == 2) {
            if (!teacherService.verifyExpertAuth(gh, type)) {
                return Result.failure(ResultCode.USER_AUTH_FAIL);
            }
            nav_type++;
        }
        //判断是否管理员
        //管理员权限分为查询与操作管理员
        if (type == 3) {
            if (!teacherService.verifyAdminAuth(gh, type)) {
                return Result.failure(ResultCode.USER_AUTH_FAIL);
            }
            //基本院查询管理员为3
            nav_type = 3;
            Admin admin = adminService.getAdminByGh(gh);
            Integer depId = admin.getDepId();
            //查询管理员此处为0，操作管理员此处为1
            Integer adminType = admin.getType();
            //校查询管理员为5
            if (depId == 0) {
                nav_type = 5;
            }
            //暨权限分为 3（院查） 4（院操） 5（校查） 6（校操）
            nav_type += adminType;
        }
        // 数据库中对应的
        return Result.success(ResultCode.SUCCESS, navMapper.getNavsByType(nav_type));
    }
}
